package com.techwells.applicationMarket.util.moac;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


import com.google.gson.Gson;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import com.googlecode.jsonrpc4j.spring.rest.JsonRpcRestClient;

/**
 * MOAC钱包的工具类
 * @author 陈加兵
 *
 */
public class MoacUtils {
	private final static String SERVER_ADDRESS="http://47.92.101.153:8545"; //服务器的地址
	
	private final static String MC_GETBALANCE="mc_getBalance";   //查询余额的方法
	
	private final static String MC_GETTRANSACTIONBYHASH="mc_getTransactionByHash";  //获取交易信息
	
	private static Map<Character, Integer> map=new HashMap<Character, Integer>();
	
	static{
		map.put('a', 10);
		map.put('b', 11);
		map.put('c', 12);
		map.put('d', 13);
		map.put('e', 14);
		map.put('f', 15);
	}
	
	/**
	 * 获取钱包的余额
	 * @param address 钱包的地址
	 * @return
	 * @throws Throwable 
	 */
	public static String getBalance(String address) throws Throwable{
		JsonRpcHttpClient client=new JsonRpcHttpClient(new URL(SERVER_ADDRESS));
		String[] params=new String[2];    //封装请求参数
		params[0]=address;  //钱包的地址  
		params[1]="latest";   //获取最新的余额信息
		String b=client.invoke(MC_GETBALANCE, params,String.class);  //返回十六进制的余额
		return getFromHexStr(b.substring(2));
//		Long str=Long.parseLong(b.substring(2),16);
//		return String.valueOf(Long.parseLong(b.substring(2),16)/1000000000000000000.0);
	}
	
	
	/**
	 * 根据hash获取交易的详细信息
	 * @throws MalformedURLException 
	 * @param hash  交易的hash值
	 * 							0xe953d6bbccab1977ae7dff6a4094f3dbbd774fb60c6b214a294c17a8c5c56156
	 */
	public static TransactionDetail getTransactionDetail(String hash) throws Throwable{
		JsonRpcHttpClient client=new JsonRpcHttpClient(new URL(SERVER_ADDRESS));
		Object[] params=new Object[1];    //封装请求参数
		params[0]=hash;
		TransactionDetail detail=client.invoke(MC_GETTRANSACTIONBYHASH, params,TransactionDetail.class);
		if (detail!=null) {
			detail.setBlockNumber(String.valueOf(detail.getBlockNumber()!=null?Integer.parseInt(detail.getBlockNumber().substring(2),16):null));
			detail.setValue(String.valueOf(new BigInteger(detail.getValue().substring(2), 16).longValue()/1000000000000000000.0));
			detail.setGasPrice(String.valueOf(Integer.parseInt(detail.getGasPrice().substring(2),16)));
			detail.setGas(String.valueOf(Integer.parseInt(detail.getGas().substring(2),16)));
		}
		return detail;
	}
	
	
	//16345785d8a0000
	public void test1() throws Throwable{
		TransactionDetail detail=getTransactionDetail("0xc5e4c2bf7a6bc1681d7a7c6b6285be043092e7fbfa59936fd75776d0788b6ef0");
		System.out.println(detail.getValue());
		System.out.println((Double)(Long.parseLong(detail.getGas())/1000000000000000000.0*Long.parseLong(detail.getGasPrice())));
		System.out.println(detail.getGasPrice());
		System.out.println(detail.getBlockNumber());
	}
	
	
	/**
	 * 16进制直接转换成为字符串
	 * @param hexStr
	 * @return
	 */
	public static String getFromHexStr(String hex){
		Set<Character> keySet = map.keySet();
		BigDecimal sum=new BigDecimal(0);  //和
		int index = hex.length()-1;
		for (int i = 0; i<hex.length(); i++) {
			if (keySet.contains(hex.charAt(i))) {
				Integer value = map.get(hex.charAt(i));
				double pow = Math.pow(16, index);
				BigDecimal multiply = new BigDecimal(value).multiply(new BigDecimal(pow));
				sum=sum.add(multiply);
			}else {
				sum=sum.add(new BigDecimal(hex.substring(i,i+1)).multiply(new BigDecimal(Math.pow(16, index))));  //相加
			}
			index--;
		}
		return sum.divide(new BigDecimal(1000000000000000000.0)).toString();
	}
	
	public void test2()throws Throwable{
		String value=getBalance("0x842979507ca3dbb94392fb076f2555c7ff483d78");
		System.out.println(value);
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
